package xin.pms.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import xin.core.jpa.JpaConstant;
import xin.core.jpa.model.SimpleSortableEntity;
import xin.pms.enums.PermissionType;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 * Permission Model
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 14:51
 */
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "pms_permission")
@Data
@ToString(exclude={"users","permissions","groups"})
@EqualsAndHashCode(of = { "code" }, callSuper = false)
public class Permission extends SimpleSortableEntity implements Serializable ,Comparable<Permission>{

    @Id
    @Column(nullable = false, length = JpaConstant.CommonStringLength)
    private String code;

    @Enumerated(EnumType.STRING)
    @Column(name = "permission_type",length = JpaConstant.EnumStringLargeColumnLength)
    private PermissionType permissionType = PermissionType.cust;

    @JsonIgnore
    @ManyToMany(mappedBy = "permissions")
    private List<User> users;

    @JsonIgnore
    @ManyToMany(mappedBy = "permissions")
    private List<Role> roles;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(updatable = false)
    protected Date createTime;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(insertable = false)
    protected Date lastModifyTime;


    @Override
    public int compareTo(Permission o) {
        return this.priority.compareTo(o.getPriority());
    }
}

